package com.black.app.service.impl.exercise;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.exercise.AppExerciseSeries;
import com.black.app.domain.exercise.AppExerciseType;
import com.black.app.domain.req.exercise.add.AppExerciseSeriesAddReq;
import com.black.app.domain.req.exercise.edit.AppExerciseSeriesEditReq;
import com.black.app.domain.req.exercise.query.AppExerciseSeriesQueryReq;
import com.black.app.domain.resp.exercise.AdminExerciseSeriesResp;
import com.black.app.domain.resp.exercise.AppExerciseSeriesResp;
import com.black.app.service.exercise.AppExerciseSeriesService;
import com.black.app.mapper.exercise.AppExerciseSeriesMapper;
import com.black.app.service.exercise.AppExerciseTypeService;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.utils.MessageUtils;
import com.black.common.utils.OSSUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author L
 * @description 针对表【app_exercise_series(锻炼系列)】的数据库操作Service实现
 * @createDate 2024-10-23 16:57:08
 */
@Service
public class AppExerciseSeriesServiceImpl extends ServiceImpl<AppExerciseSeriesMapper, AppExerciseSeries>
        implements AppExerciseSeriesService {

    @Autowired
    private AppExerciseTypeService appExerciseTypeService;

    /**
     * 根据类型id查询前两条数据
     *
     * @param typeId
     * @return
     */
    @Override
    public PageInfo<AppExerciseSeries> findByTidTop2(Long typeId) {
        return PageHelper.startPage(1, 2).doSelectPageInfo(() -> {
            //构建sql查询条件
            LambdaQueryWrapper<AppExerciseSeries> wrapper = new LambdaQueryWrapper<>();
            // where t_id=?
            wrapper.eq(AppExerciseSeries::getTId, typeId);
            this.list(wrapper);
        });

    }

    @Override
    public List<AppExerciseSeries> findByTidAll(Long typeId) {
        //构建sql查询条件
        LambdaQueryWrapper<AppExerciseSeries> wrapper = new LambdaQueryWrapper<>();
        // where t_id=?
        wrapper.eq(AppExerciseSeries::getTId, typeId);
        return this.list(wrapper);
    }

    /**
     * 根据id查询
     *
     * @param sId
     * @return
     */
    @Override
    public AdminExerciseSeriesResp findById(Long sId) {

        if (Objects.isNull(sId)) {
            throw new PrimaryException();
        }
        AdminExerciseSeriesResp appExerciseSeries = this.baseMapper.findById(sId);

        return appExerciseSeries;
    }

    /**
     * 分页查询-后台
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<AdminExerciseSeriesResp> paginationQuery(AppExerciseSeriesQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 根据id删除
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }

        for (Long id : ids) {
            AppExerciseSeries appExerciseSeries = this.getById(id);

            if (Objects.nonNull(appExerciseSeries)) {
                // 判断当前系列下是否还有动作分组
                if (this.baseMapper.findGroupBySid(id).size() > 0) {
                    throw new ServiceException("部分系列下还有动作分组，无法删除");
                }
                // oss删除图片
                OSSUtils.deleteFile(appExerciseSeries.getPath());
                this.removeById(id);
            }
        }
        return true;
    }

    /**
     * 新增系列
     *
     * @param req
     * @return
     */
    @Override
    public boolean add(AppExerciseSeriesAddReq req) {
        // 校验 锻炼标题是否存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppExerciseSeries>().eq(AppExerciseSeries::getTitle, req.getTitle()))) {
            // 删除已上传的图片
            OSSUtils.deleteFile(req.getPath());
            throw new ServiceException(MessageUtils.message("appexerciseseries.title.exists"));
        }
        // 校验 锻炼类型是否存在
        AppExerciseType type = this.appExerciseTypeService.findById(req.getTId());
        if (Objects.isNull(type) && type.getTypeName().equals(req.getTypeName())) {
            // 删除已上传的图片
            OSSUtils.deleteFile(req.getPath());
            throw new ServiceException(MessageUtils.message("appexercisetype.not.exists"));
        }

        AppExerciseSeries appExerciseSeries = new AppExerciseSeries();
        BeanUtils.copyProperties(req, appExerciseSeries);

        boolean save = this.save(appExerciseSeries);

        return save;
    }

    /**
     * 修改系列
     *
     * @param req
     * @return
     */
    @Override
    public boolean edit(AppExerciseSeriesEditReq req) {

        if (Objects.isNull(req.getSeriesId())) {
            throw new PrimaryException();
        }
        // 校验 锻炼标题是否存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppExerciseSeries>().eq(AppExerciseSeries::getTitle, req.getTitle()).ne(AppExerciseSeries::getSeriesId, req.getSeriesId()))) {
            // 删除已上传的图片
            OSSUtils.deleteFile(req.getPath());
            throw new ServiceException(MessageUtils.message("appexerciseseries.title.exists"));
        }
        // 校验 锻炼类型是否存在
        if (this.appExerciseTypeService.findById(req.getTId()) == null) {
            // 删除已上传的图片
            OSSUtils.deleteFile(req.getPath());
            throw new ServiceException(MessageUtils.message("appexercisetype.not.exists"));
        }

        AppExerciseSeries appExerciseSeries = this.getById(req.getSeriesId());

        if (Objects.isNull(appExerciseSeries)) {
            throw new ServiceException(MessageUtils.message("appexerciseseries.not.exists"));
        }

        // 删除原本的图片
        if (!appExerciseSeries.getPath().equals(req.getPath())){
            OSSUtils.deleteFile(appExerciseSeries.getPath());
        }

        BeanUtils.copyProperties(req, appExerciseSeries);

        boolean res = this.updateById(appExerciseSeries);

        return res;
    }
}




